package com.hds.modules.app.entity.wx.po;

import java.util.HashMap;
import java.util.Map;

import com.hds.modules.app.entity.wx.common.CommonUtils;
import com.hds.modules.app.entity.wx.common.WxConstants;
import com.hds.modules.app.entity.wx.utils.WxPayUtils;
/**
 * 统一下单接口返回结果数据类
 * @version 1.0
 */
public class UnifiedOrderResult {

    /**
     * 返回代码
     * SUCCESS-下单成功/FAIL-下单失败
     */
    private String resultCode = "";

    /**
     * 返回信息
     */
    private String resultMsg = "";

    /**
     * 预支付ID
     * 下单成功成功后微信返回预支付ID，
     * 用于页面调用支付请求使用
     */
    private String prepayId = "";

    /**
     * 公众号ID
     * 用于页面调用支付请求使用
     */
    private String appId = "";

    /**
     * 时间戳
     * 用于页面调用支付请求使用
     */
    private String timeStamp = "";

    /**
     * 随机字符串
     * 用于页面调用支付请求使用
     */
    private String nonceStr = "";

    /**
     * 签名类型
     * 默认MD5
     * 用于页面调用支付请求使用
     */
    private String signType = "MD5";

    /**
     * 支付签名
     * 用于页面调用支付请求使用
     */
    private String paySign = "";

    String orderAmount= "";  		//订单金额
    String orderId= "";  		//订单ID
    String payableAmount= "";		//应支付金额

    public String getResultCode() {
        return resultCode;
    }

    public void setResultCode(String resultCode) {
        this.resultCode = resultCode;
    }

    public String getResultMsg() {
        return resultMsg;
    }

    public void setResultMsg(String resultMsg) {
        this.resultMsg = resultMsg;
    }

    public String getPrepayId() {
        return prepayId;
    }

    public void setPrepayId(String prepayId) {
        this.prepayId = prepayId;
    }

    public String getAppId() {
        return appId;
    }

    public void setAppId(String appId) {
        this.appId = appId;
    }

    public String getTimeStamp() {
        return timeStamp;
    }

    public void setTimeStamp(String timeStamp) {
        this.timeStamp = timeStamp;
    }

    public String getNonceStr() {
        return nonceStr;
    }

    public void setNonceStr(String nonceStr) {
        this.nonceStr = nonceStr;
    }

    public String getSignType() {
        return signType;
    }

    public void setSignType(String signType) {
        this.signType = signType;
    }

    public String getPaySign() {
        return paySign;
    }

    public void setPaySign(String paySign) {
        this.paySign = paySign;
    }

    public String getOrderAmount() {
        return orderAmount;
    }

    public void setOrderAmount(String orderAmount) {
        this.orderAmount = orderAmount;
    }

    public String getPayableAmount() {
        return payableAmount;
    }

    public void setPayableAmount(String payableAmount) {
        this.payableAmount = payableAmount;
    }

    public String getOrderId() {
        return orderId;
    }

    public void setOrderId(String orderId) {
        this.orderId = orderId;
    }

    /**
     * 获取返回给业务层使用的微信统一下单返回数据
     * @param wxunifiedOrderResult
     * @param apiKey
     * @return
     */
    public UnifiedOrderResult getUnifiedOrderResult(WxUnifiedOrderResult wxunifiedOrderResult,String apiKey)
    {
        if(WxConstants.SUCCESS.equals(wxunifiedOrderResult.getReturn_code())
                && WxConstants.SUCCESS.equals(wxunifiedOrderResult.getResult_code()))
        {
            this.setResultCode(WxConstants.SUCCESS);
            this.setResultMsg(WxConstants.SUCCESS);
            this.setPrepayId(wxunifiedOrderResult.getPrepayId());
            this.setAppId(wxunifiedOrderResult.getAppId());
            this.setNonceStr(CommonUtils.getRandomStr(32));
            this.setTimeStamp(String.valueOf(System.currentTimeMillis()/1000));
            String sign = this.getJsSDKPaySign(this, apiKey);
            this.setPaySign(sign);

        }else{
            if(WxConstants.FAIL.equals(wxunifiedOrderResult.getReturn_code())){
                this.setResultCode(WxConstants.FAIL);
                this.setResultMsg(wxunifiedOrderResult.getReturn_msg());
            }else if(WxConstants.FAIL.equals(wxunifiedOrderResult.getResult_code())){
                this.setResultCode(WxConstants.FAIL);
                this.setResultMsg(wxunifiedOrderResult.getErr_code_des());
            }
        }
//        System.out.println("UnifiedOrderResult"+this.getResultCode()+" "+this.getResultMsg());

        return this;
    }

    /**
     * 获取页面发起支付请求所需签名
     * @param getUnifiedOrderResult
     */
    public String getJsSDKPaySign(UnifiedOrderResult unifiedOrderResult,String apiKey){
        //支付签名
        String[] paramNames = new String[]{
                "appId","timeStamp","nonceStr","package","signType"
        };
        Map<String,String> paramValueMap = new HashMap<String,String>();
        paramValueMap.put("appId",unifiedOrderResult.getAppId());
        paramValueMap.put("timeStamp", unifiedOrderResult.getTimeStamp());
        paramValueMap.put("nonceStr", unifiedOrderResult.getNonceStr());
        paramValueMap.put("package","prepay_id="+unifiedOrderResult.getPrepayId());
        paramValueMap.put("signType", unifiedOrderResult.getSignType());
        paramValueMap.put(WxConstants.APIKEY, apiKey);

        String sign = WxPayUtils.getPaySignMD5(paramNames,paramValueMap);

        return sign;
    }

}